/**********************************************************************

 .-') _   _  .-')                         ('-.     _   .-')
(  OO) ) ( \( -O )                       ( OO ).-.( '.( OO )_
/     '._ ,------.  ,-.-')   ,----.      / . --. / ,--.   ,--.) ,-.-')
|'--...__)|   /`. ' |  |OO) '  .-./-')   | \-.  \  |   `.'   |  |  |OO)
'--.  .--'|  /  | | |  |  \ |  |_( O- ).-'-'  |  | |         |  |  |  \
   |  |   |  |_.' | |  |(_/ |  | .--, \ \| |_.'  | |  |'.'|  |  |  |(_/
   |  |   |  .  '.',|  |_.'(|  | '. (_/  |  .-.  | |  |   |  | ,|  |_.'
   |  |   |  |\  \(_|  |    |  '--'  |   |  | |  | |  |   |  |(_|  |
   `--'   `--' '--' `--'     `------'    `--' `--' `--'   `--'  `--'


*********************************************************************/

#include <iostream>
#include <stdlib.h>
#include <vector>

#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"

#include "include/main.h"
#include "include/SDLTimer.h"

using namespace std;

///////////////////////Variables//////////////////////////////

//Screen surface (used for resize)
SDL_Surface *surf;
SDL_Event event;
const int FRAMES_PER_SECOND = 30;
const int SCREEN_BPP = 32;
//Quit variable for entire game
bool quit = false;

//Minion TestMinion ( Position(0,-10)); //used to test for walling off minion paths and if minion path chages due to turret placement
 // Path vector used for TestMinion

int _scrWidth = 0;
int _scrHeight = 0;
int _scrRatio = 0;
///////////////////////End Variables//////////////////////////

///////////////////////Fuction Defines////////////////////////
void handleResize(GLsizei,GLsizei);
void gamePlay();
void game();
void clean_up();
////////////////////End Function Defines//////////////////////

////////////////////////Functions/////////////////////////////

//handleEvents() - main event handler for game
void handleEvents()
{

           //While there are events to handle
        while( SDL_PollEvent( &event ) )
        {
            switch(event.type)
            {
            case SDL_QUIT:
                quit = true;
                break;
            case SDL_VIDEORESIZE:
                SDL_FreeSurface(surf);
                _scrWidth = event.resize.w;
                _scrHeight = _scrWidth/_scrRatio;
                surf = SDL_SetVideoMode(_scrWidth,_scrHeight,SCREEN_BPP,SDL_OPENGL|SDL_RESIZABLE);
                handleResize(_scrWidth,_scrHeight);
                break;
            case SDL_MOUSEMOTION:
                break;
            default:
                break;
        }

    }


}

//Initializes 3D rendering
void initRendering()
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING); //Enable lighting
    glEnable(GL_LIGHT0); //Enable light #0
    glEnable(GL_LIGHT1); //Enable light #1
    glEnable(GL_NORMALIZE); //Automatically normalize normals
    //glShadeModel(GL_SMOOTH); //Enable smooth shading
    glEnable (GL_BLEND);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

//Called when the window is resized
void handleResize(GLsizei w,GLsizei h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

//Draws the 3D scene
void drawScene()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    SDL_GL_SwapBuffers();
}

//Contains update for minion, and hero functions
void update()
{

}


//SDL and other Initialization junk
bool init()
{
    _scrWidth = 640*2;
    _scrHeight = 320*2;
    _scrRatio = _scrWidth/_scrHeight;
    //Initialize SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        return false;
    }
    freopen( "CON", "w", stdout );
    freopen( "CON", "w", stderr );
    //Create Window
    if( (surf = SDL_SetVideoMode( _scrWidth, _scrHeight, SCREEN_BPP, SDL_OPENGL | SDL_RESIZABLE )) == NULL )
    {
        return false;
    }
    handleResize(_scrWidth,_scrHeight);
    initRendering();
    //Set caption
    SDL_WM_SetCaption( "BankRobber3", NULL );
    return true;
}

void game()
{
    // find out which game mode we are in and call the appropiate routine
    gamePlay();

}

//Function of the actual game!
void gamePlay()
{
    update();
    handleEvents();
    drawScene();
}

int main(int argc, char** argv)
{


    if( init() == false )
    {
        return 1;
    }





    //The frame rate regulator
    SDLTimer fps;
    fps.start();

    //Wait for user exit
    while( quit == false )
    {
        //Start the frame timer
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
        game();
    }

    //Clean up
    clean_up();

    return 0;
}

void clean_up()
{
    //Quit SDL
    SDL_Quit();
}
